const http = require('../../utils/request')
const { userApi } = require('../../utils/api')

Page({
  data: {
    phone: '',
    verifyCode: '',
    countDown: 0,
    performerNo: '17358603617', // 修改演示账号
    fixedCode: '12345' // 固定验证码
  },

  // 输入手机号
  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 输入验证码
  onVerifyCodeInput(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },

  // 获取验证码
  getVerifyCode() {
    if(this.data.countDown > 0) return
    
    const phone = this.data.phone
    if(!/^1[3-9]\d{9}$/.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return
    }

    // 使用封装的请求方法
    http.get(userApi.sendCode(phone))
      .then(() => {
        wx.showToast({
          title: '验证码已发送',
          icon: 'success'
        })
        this.setData({ countDown: 60 })
        this.startCountDown()
      })
      .catch(err => {
        wx.showToast({
          title: err || '发送失败',
          icon: 'none'
        })
      })
  },

  // 倒计时
  startCountDown() {
    const timer = setInterval(() => {
      if(this.data.countDown <= 1) {
        clearInterval(timer)
      }
      this.setData({
        countDown: this.data.countDown - 1
      })
    }, 1000)
  },

  // 登录
  onLogin() {
    const { phone, verifyCode } = this.data
    
    if(!/^1[3-9]\d{9}$/.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return
    }

    if(!verifyCode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }

    // 使用封装的请求方法
    http.post(userApi.login, {
      mobile: phone,
      code: verifyCode,
      loginType: 1
    }).then(res => {
      if(res.success) {
        // 保存token和用户信息
        wx.setStorageSync('token', res.token)
        wx.setStorageSync('userInfo', {
          userId: res.userId,
          userName: res.userName,
          roleCode: res.roleCode,
          regionId: res.regionId,
          isRepair: res.repair
        })
        
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1500,
          success: () => {
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/index/index'
              })
            }, 1500)
          }
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err || '登录失败',
        icon: 'none'
      })
    })
  },

  // 设置服务器地址
  onSetUrl() {
    wx.showModal({
      title: '设置服务器地址',
      editable: true,
      placeholderText: '请输入服务器地址(https://)',
      content: wx.getStorageSync('serverUrl') || 'https://localhost:9007',
      success: (res) => {
        if(res.confirm) {
          // 验证是否是https地址
          if(!res.content.startsWith('https://')) {
            wx.showToast({
              title: '请输入https地址',
              icon: 'none'
            })
            return
          }
          wx.setStorageSync('serverUrl', res.content)
          wx.showToast({
            title: '设置成功',
            icon: 'success'
          })
        }
      }
    })
  }
}) 